package com.jiao.service.impl;

import com.jiao.bean.TBao;
import com.jiao.bean.TBaoQuery;
import com.jiao.dao.TBaoDao;
import com.jiao.service.TBaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class TBaoServiceImpl implements TBaoService {

    @Autowired
    TBaoDao tBaoDao;

    @Override
    public List<TBao> query(TBaoQuery tBaoQuery) {
        return tBaoDao.selectByExample(tBaoQuery);
    }

    @Override
    public int insert(@RequestBody @Valid TBao tBao) {
        //日期处理 日期两种格式分别根据数据库信息和页面查询的时候入参
        tBao.setCreatedate(new java.sql.Date(new Date().getTime()));
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        tBao.setYear(year);
        tBao.setMonth(month);
        return tBaoDao.insertSelective(tBao);
    }

    @Override
    public int update(TBao tBao) {
        return tBaoDao.updateByPrimaryKeySelective(tBao);
    }

    @Override
    public int delete(Integer id) {
        return tBaoDao.deleteByPrimaryKey(id);
    }

    @Override
    public int countByTime(TBaoQuery tBaoQuery) {
        return tBaoDao.countByExample(tBaoQuery);
    }

    @Override
    public TBao queryById(Integer id) {
        return tBaoDao.selectByPrimaryKey(id);
    }
}
